package com.tianpengtech.admin.authenticate;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.admin.AbstractAdminController;
import com.tianpengtech.admin.IAdminOpt;
import com.tianpengtech.admin.InitInterceptor;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.feedback.FeedBack;
import com.tianpengtech.common.model.opertion.Opertion;
import com.tianpengtech.common.model.task.Task;
import com.tianpengtech.common.util.JsonUtil;

/**
* 路由配置地址：add("/admin/authenticate", AuthenticateAdminController.class, "/admin/authenticate");
* 数据库映射地址：arp.addMapping(Authenticate.dao.tableName(),Authenticate.class);
*/
@Before(InitInterceptor.class)
@RequiresAuthentication
public class AuthenticateAdminController extends AbstractAdminController{
	private IAdminOpt opt = new AuthenticateAdminServiceImpl();
	@Override
	protected void config() {
		this.clazz=Authenticate.class;
		this.service=new AuthenticateAdminServiceImpl();
	}

	@Override
	public void beforeAdd() {

	}

	@Override
	public void beforeAdd(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model< ? > model) throws ValidateException {

	}

	@Override
	public void beforeDelete(Model< ? > model) {

	}

	
	/**
	 * 审核通过
	 */
	public void  pass(){
		int id = getParaToInt(Authenticate.ID,0);
		try {
			opt.changeStatus(getModel(Authenticate.class), id, Authenticate.STATUS_SUCCESS, Kv.create());
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}
	
	/**
	 * 拒绝理由填写页面
	 */
	public void  toRefuse(){
		int id = getParaToInt(Authenticate.ID,0);
		setAttr("authenticate",this.service.queryById(getModel(Authenticate.class), id));
		renderTemplate("authenticate-reason.html");
	}
	
	/**
	 * 审核不通过
	 */
	public void  unpass(){
		Authenticate authenticate = getModel(Authenticate.class);
		String content = getPara(Opertion.CONTENT);
		Kv kv = Kv.by(Opertion.CONTENT, content);
		try {
			opt.changeStatus(authenticate, authenticate.getLong(Authenticate.ID), Authenticate.STATUS_FAILURE, kv);
			renderJson(JsonUtil.printSuccess("操作成功"));
		} catch (ValidateException e) {
			renderJson(JsonUtil.printError(e.getMessage()));
		}
	}

	@Override
	public void parsePageRecord(Object results) {
		
		Authenticate authenticate = (Authenticate)results;
		authenticate.set(Authenticate.STATUS,authenticate.status());
	}
	
	
	public void showDetail(){
		int id = getParaToInt(Authenticate.ID, GlobalConfigConstant.DEFAULT_VALUE_ZERO);
		setAttr("authenticate", this.service.queryById(getModel(Authenticate.class), id));
		renderTemplate("authenticate-detail.html");
	}
	
}

	